package com.synnex.wmsdeployer.ui;


import java.net.MalformedURLException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import com.synnex.wmsdeployer.log.SimpleLogger;
import com.synnex.wmsdeployer.util.PropertyUtil;


public class TestDerby {
	private static Connection conn;
	static{
   	 try {
		Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
		conn= DriverManager.getConnection("jdbc:derby:DeployLog;create=true");
	} catch (Throwable e) {
		e.printStackTrace();
	} 
   
	}
    public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, InstantiationException, SQLException, MalformedURLException {
        Connection conn = getConnection();
        Statement st = conn.createStatement();
        if ("true".equalsIgnoreCase(PropertyUtil.getValue("cleanDB"))) {
			try {
				st.executeUpdate("drop table deploylog");
				//        	st.execute("create table deploylog(ID INT NOT NULL GENERATED ALWAYS AS IDENTITY (START WITH 1, INCREMENT BY 1), deployUser VARCHAR(100), deployUserIp VARCHAR(1000), deployRecordFilePath VARCHAR(1000) NOT NULL, deployDate timestamp, deployLogFileName VARCHAR(1000) NOT NULL,deployFileName VARCHAR(1000) NOT NULL, deployTargetJar VARCHAR(1000) NOT NULL,deployClassName  VARCHAR(3000), deployResult INT NOT NULL)");
				System.err.println(PropertyUtil.getValue("tableDDL"));
				st.execute(PropertyUtil.getValue("tableDDL"));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		PreparedStatement pst=conn.prepareStatement("insert into deploylog(deployUser,deployUserIp,deployRecordFilePath,deployDate,deployLogFileName,deployFileName,deployTargetJar,deployClassName,deployResult) values ('TOM','TOM','TOM',?,'TOM','TOM','TOM','TOM',1)");
        pst.setTimestamp(1,new Timestamp(System.currentTimeMillis()));
        pst.execute();
//        st.executeUpdate("insert into USER_INFO(NAME) values ('Jack')");
        ResultSet rs = st.executeQuery("select * from deploylog");
        while (rs.next()){
            int id = rs.getInt(1);
            String name = rs.getString(2);
            SimpleLogger.addMessage(rs.getTimestamp(5).toString());
            SimpleLogger.addMessage(">>> "+id+" "+name+"\n");
        }
    }
    
    public static Connection getConnection(){
    	return conn;
    }
}
